package cn.jn.lhm.teacher.service.impl;

import cn.jn.lhm.teacher.entity.EduSubject;
import cn.jn.lhm.teacher.entity.excel.SubjectData;
import cn.jn.lhm.teacher.entity.subject.OneSubject;
import cn.jn.lhm.teacher.entity.subject.TwoSubject;
import cn.jn.lhm.teacher.listen.SubjectListener;
import cn.jn.lhm.teacher.mapper.EduSubjectMapper;
import cn.jn.lhm.teacher.service.EduService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/1 - 21:22
 */
@Service
public class EduServiceImpl implements EduService {

    @Resource
    private EduSubjectMapper eduSubjectMapper;

    @Override
    public void saveSubject(MultipartFile file, EduService eduService){
        try {
            EasyExcel.read(file.getInputStream(), SubjectData.class, new SubjectListener(eduService)).sheet().doRead();
        } catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public EduSubject querySubjectByPid(QueryWrapper<EduSubject> wrapper) {
        return eduSubjectMapper.selectOne(wrapper);
    }

    @Override
    public int save(EduSubject oneSubject) {
        return eduSubjectMapper.insert(oneSubject);
    }

    @Override
    public List<OneSubject> getSubjectTree() {
        /**
         * 还可以通过有无parent_Id来区别一级以及二级分类
         * 然后两重循环遍历一级和二级分类，组装数据
         */
        ArrayList<OneSubject> list = new ArrayList<>();
        //查询所有的一级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
        List<EduSubject> eduSubjects = eduSubjectMapper.selectList(wrapper);
        //根据一级分类来查询二级分类
        eduSubjects.forEach(subject -> {
            QueryWrapper<EduSubject> childWrapper = new QueryWrapper<>();
            childWrapper.eq("parent_id", subject.getId());
            //查询对应的二级分类
            List<EduSubject> childList = eduSubjectMapper.selectList(childWrapper);
            //组装
            list.add(packOneSubject(subject,childList));
        });
        return list;
    }

    /**
     * 组装数据
     * BeanUtils.copyProperties(obj,obj)
     * 第一参数是get的对象，第二个是set的对象
     * 第一个是源对象，第二个是目标对象
     * @param eduSubject 一级分类对应的实体类
     * @param subjectList 一级分类对应的二级分类
     * @return 返回组装的一级分类
     */
    private OneSubject packOneSubject(EduSubject eduSubject, List<EduSubject> subjectList){
        //children集合
        ArrayList<TwoSubject> subjects = new ArrayList<>();
        subjectList.forEach(subject -> {
            TwoSubject twoSubject = new TwoSubject();
            BeanUtils.copyProperties(subject, twoSubject);
            subjects.add(twoSubject);
        });
        OneSubject oneSubject = new OneSubject();
        BeanUtils.copyProperties(eduSubject, oneSubject);
        oneSubject.setChildren(subjects);
        return oneSubject;
    }
}
